package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    //创建库管对象studentdao
    private  StudentDao studentDao = new StudentDao();
    public boolean addStudent(Student stu) {
        StudentDao addStudentdao = new StudentDao();
        //将学生对象，传递到StudentDao 库管中的addstudent方法
        return  StudentDao.addstudent(stu);
        //将返回的boolean类型结果，返还到StudentController
    }

    public boolean isExists(String id) {
        Student[] stus = studentDao.findAllStudent();
        //假设id对象不存在
        boolean exists = false;
        //遍历数组，获得每一个学生对象，准备进行判断
         for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)){
                  exists = true;
                  break;
            }
        }
        return exists;
    }

    public Student[] findAllStudent() {
        //调用库管对象的indAllStudent获取学生对象数组
        Student [] allStudent = studentDao.findAllStudent();
        //判断数组中是否有学生信息（有则返回地址，没有则返回null）
        //思路：数组中只要存在一个不是null的元素，那就代表有学生信息
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            if (stu != null){
                flag = true;
                break;
            }
        }
        if (flag){
            //有信息
            return allStudent;
        }else {
            //没有信息
            return null;
        }
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
